CMAKE_MINIMUM_REQUIRED(VERSION 2.8.4)




PROJECT(ooGame)




SET(LOCAL_PROJECT_NAME ooGame)



SET(OOGAME_SOVERSION 6)
SET(OOGAME_MAJOR_VERSION 0)
SET(OOGAME_MINOR_VERSION 3)
SET(OOGAME_PATCH_VERSION 0)
SET(OOGAME_VERSION ${OOGAME_MAJOR_VERSION}.${OOGAME_MINOR_VERSION}.${OOGAME_PATCH_VERSION})



IF(NOT WIN32)
    IF(CMAKE_SIZEOF_VOID_P MATCHES "8")
        SET(LIB_POSTFIX "64" CACHE STRING "suffix for 32/64 dir placement")
        MARK_AS_ADVANCED(LIB_POSTFIX)
    ENDIF()

    SET(PKGCONFIG_FILES
            ooGame
    )

    FOREACH(PKGCONFIG_FILE ${PKGCONFIG_FILES})
        CONFIGURE_FILE(${PROJECT_SOURCE_DIR}/packaging/pkgconfig/${PKGCONFIG_FILE}.pc.in
        ${PROJECT_BINARY_DIR}/packaging/pkgconfig/${PKGCONFIG_FILE}.pc
        @ONLY
        )
    INSTALL(FILES ${PROJECT_BINARY_DIR}/packaging/pkgconfig/${PKGCONFIG_FILE}.pc DESTINATION lib${LIB_POSTFIX}/pkgconfig)
    ENDFOREACH(PKGCONFIG_FILE)
ENDIF(NOT WIN32)






CONFIGURE_FILE("${PROJECT_SOURCE_DIR}/src/ooGame/Version.cpp.in" "${PROJECT_SOURCE_DIR}/src/ooGame/Version.cpp")







OPTION(OOGAME_BUILD_PLATFORM_ANDROID "" OFF)
IF(OOGAME_BUILD_PLATFORM_ANDROID)

    SET(ANDROID TRUE)
    SET(ANDROID_PLATFORM 5)

    SET(OOGAME_ANDROID_TEMPLATES "${CMAKE_SOURCE_DIR}/PlatformSpecifics/Android")

ENDIF(OOGAME_BUILD_PLATFORM_ANDROID)









INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/include)



IF(NOT CMAKE_BUILD_TYPE)
    SET(CMAKE_BUILD_TYPE "Release")
ENDIF(NOT CMAKE_BUILD_TYPE)






SET(OOGAME_DEBUG_BUILD OFF)

IF(CMAKE_BUILD_TYPE MATCHES "Debug")
    SET(OOGAME_DEBUG_BUILD ON)
ENDIF(CMAKE_BUILD_TYPE MATCHES "Debug")









IF( WIN32 )
    ADD_DEFINITIONS(-W4 -DOSG_LIBRARY)
ELSE( WIN32 )
    ADD_DEFINITIONS(-Wall -DOSG_LIBRARY -fPIC)
ENDIF( WIN32 )





IF( NOT WIN32 )
    IF(OOGAME_DEBUG_BUILD)
        MESSAGE(STATUS "ooGame build: DEBUG")

    ELSE(OOGAME_DEBUG_BUILD)
        MESSAGE(STATUS "ooGame build: RELEASE")

    ENDIF(OOGAME_DEBUG_BUILD)
ENDIF( NOT WIN32 )




CONFIGURE_FILE("${PROJECT_SOURCE_DIR}/include/ooGame/Config.in" "${PROJECT_SOURCE_DIR}/include/ooGame/Config")





IF( CMAKE_SIZEOF_VOID_P EQUAL 8 )

    ADD_DEFINITIONS(-DIS_X86_64=1)

ELSE( CMAKE_SIZEOF_VOID_P EQUAL 8 )

    ADD_DEFINITIONS(-DIS_X86_64=0)

ENDIF( CMAKE_SIZEOF_VOID_P EQUAL 8 )






IF( WIN32 )
    SET(OSG_INCLUDE "$ENV{PROGRAMFILES}\\OpenSceneGraph\\include" CACHE PATH "OSG Headers")
    SET(OSGODE_INCLUDE "$ENV{PROGRAMFILES}\\osgODE\\include" CACHE PATH "osgODE Headers")
    SET(PSOUND_INCLUDE "$ENV{PROGRAMFILES}\\pSound\\include" CACHE PATH "pSound Headers")

ELSE( WIN32 )

    IF( NOT ANDROID )

        EXECUTE_PROCESS(COMMAND pkg-config openscenegraph-osg --cflags-only-I OUTPUT_VARIABLE tmp_var)
        STRING(REGEX REPLACE "\n" "" tmp_var "${tmp_var}")
        STRING(REGEX REPLACE "-I" "" tmp_var "${tmp_var}")

        SET(OSG_INCLUDE ${tmp_var} CACHE PATH "OSG Headers")



        EXECUTE_PROCESS(COMMAND pkg-config osgODE --cflags-only-I OUTPUT_VARIABLE tmp_var)
        STRING(REGEX REPLACE "\n" "" tmp_var "${tmp_var}")
        STRING(REGEX REPLACE "-I" "" tmp_var "${tmp_var}")

        SET(OSGODE_INCLUDE ${tmp_var} CACHE PATH "osgODE Headers")



        EXECUTE_PROCESS(COMMAND pkg-config pSound --cflags-only-I OUTPUT_VARIABLE tmp_var)
        STRING(REGEX REPLACE "\n" "" tmp_var "${tmp_var}")
        STRING(REGEX REPLACE "-I" "" tmp_var "${tmp_var}")

        SET(PSOUND_INCLUDE ${tmp_var} CACHE PATH "pSound Headers")

    ENDIF( NOT ANDROID )

ENDIF( WIN32 )


MESSAGE(STATUS "OSG include: ${OSG_INCLUDE}")
MESSAGE(STATUS "osgODE include: ${OSGODE_INCLUDE}")

IF( NOT ANDROID )
    MESSAGE(STATUS "pSound include: ${PSOUND_INCLUDE}")
ENDIF( NOT ANDROID )





IF( WIN32 )
    SET(OSG_LIBRARIES "$ENV{PROGRAMFILES}\\OpenSceneGraph\\lib" CACHE PATH "OSG Libraries")
    SET(OSGODE_LIBRARIES "$ENV{PROGRAMFILES}\\osgODE\\lib" CACHE PATH "osgODE Libraries")
    SET(PSOUND_LIBRARIES "$ENV{PROGRAMFILES}\\pSound\\lib" CACHE PATH "pSound Libraries")

ELSE( WIN32 )

    IF( NOT ANDROID )

        EXECUTE_PROCESS(COMMAND pkg-config openscenegraph-osg --libs-only-L OUTPUT_VARIABLE tmp_var)
        STRING(REGEX REPLACE "\n" "" tmp_var "${tmp_var}")
        STRING(REGEX REPLACE "-L" "" tmp_var "${tmp_var}")

        SET(OSG_LIBRARIES ${tmp_var} CACHE PATH "OSG libraries")



        EXECUTE_PROCESS(COMMAND pkg-config osgODE --libs-only-L OUTPUT_VARIABLE tmp_var)
        STRING(REGEX REPLACE "\n" "" tmp_var "${tmp_var}")
        STRING(REGEX REPLACE "-L" "" tmp_var "${tmp_var}")

        SET(OSGODE_LIBRARIES ${tmp_var} CACHE PATH "osgODE libraries")



        EXECUTE_PROCESS(COMMAND pkg-config pSound --libs-only-L OUTPUT_VARIABLE tmp_var)
        STRING(REGEX REPLACE "\n" "" tmp_var "${tmp_var}")
        STRING(REGEX REPLACE "-L" "" tmp_var "${tmp_var}")

        SET(PSOUND_LIBRARIES ${tmp_var} CACHE PATH "pSound libraries")

    ENDIF( NOT ANDROID )

ENDIF( WIN32 )


MESSAGE(STATUS "OSG libraries: ${OSG_LIBRARIES}")
MESSAGE(STATUS "osgODE libraries: ${OSGODE_LIBRARIES}")

IF( NOT ANDROID )
    MESSAGE(STATUS "pSound libraries: ${PSOUND_LIBRARIES}")
ENDIF( NOT ANDROID )





IF( WIN32 )
    SET(OSG_BINARIES "$ENV{PROGRAMFILES}\\OpenSceneGraph\\bin" CACHE PATH "OSG binaries")
    MESSAGE(STATUS "OSG binaries: ${OSG_BINARIES}")
ENDIF( WIN32 )




IF(NOT ANDROID)
    INCLUDE_DIRECTORIES(${OSG_INCLUDE})
    INCLUDE_DIRECTORIES(${OSGODE_INCLUDE})
    INCLUDE_DIRECTORIES(${PSOUND_INCLUDE})

    LINK_DIRECTORIES( ${OSG_LIBRARIES} )
    LINK_DIRECTORIES( ${OSGODE_LIBRARIES} )
    LINK_DIRECTORIES( ${PSOUND_LIBRARIES} )
ENDIF(NOT ANDROID)





CONFIGURE_FILE(
    "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules/cmake_uninstall.cmake.in"
    "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
    IMMEDIATE @ONLY)
ADD_CUSTOM_TARGET(uninstall
    "${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake")





SET(OOGAME_DOXYGEN_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/doc" CACHE PATH "Doxygen output directory")



SET(OOGAME_TEMP_DOXYGEN_INPUT "${PROJECT_BINARY_DIR}/doc/oogame_doxygen_input_files.txt")
MARK_AS_ADVANCED(OOGAME_TEMP_DOXYGEN_INPUT)

FILE(WRITE ${OOGAME_TEMP_DOXYGEN_INPUT} "${PROJECT_SOURCE_DIR}/README\n")


FIND_PACKAGE(Doxygen)

IF(DOXYGEN_FOUND)
    ADD_CUSTOM_TARGET(  doc
                        ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/doc/Doxyfile
                        WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
                        COMMENT "Generating API documentation with Doxygen"
                        VERBATIM
    )
ENDIF(DOXYGEN_FOUND)




SET(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/CMakeModules;${CMAKE_MODULE_PATH}")

INCLUDE(ooGameMacros)

ADD_SUBDIRECTORY(src)
ADD_SUBDIRECTORY(doc)


IF( NOT ANDROID )
    OPTION(BUILD_OOGAME_EXAMPLES "Build ooGame examples" OFF)
    MESSAGE(STATUS "ooGame examples: ${BUILD_OOGAME_EXAMPLES}")


    IF( BUILD_OOGAME_EXAMPLES )
        ADD_SUBDIRECTORY( examples )
    ENDIF( BUILD_OOGAME_EXAMPLES )
ENDIF( NOT ANDROID )



IF( NOT ANDROID )
    OPTION(BUILD_OOGAME_APPLICATIONS "Build ooGame applications" ON)
    MESSAGE(STATUS "ooGame applications: ${BUILD_OOGAME_APPLICATIONS}")


    IF( BUILD_OOGAME_APPLICATIONS )
        ADD_SUBDIRECTORY( applications )
    ENDIF( BUILD_OOGAME_APPLICATIONS )
ENDIF( NOT ANDROID )




FILE(REMOVE ${OOGAME_TEMP_DOXYGEN_INPUT})




MESSAGE(STATUS "${PROJECT_NAME} install prefix: ${CMAKE_INSTALL_PREFIX}")





IF(ANDROID)
    CONFIGURE_FILE("${OOGAME_ANDROID_TEMPLATES}/Android.mk.master.in" "${CMAKE_BINARY_DIR}/Android.mk")
    CONFIGURE_FILE("${OOGAME_ANDROID_TEMPLATES}/Application.mk.master.in" "${CMAKE_BINARY_DIR}/Application.mk")
    CONFIGURE_FILE("${OOGAME_ANDROID_TEMPLATES}/AndroidManifest.xml.master.in" "${CMAKE_BINARY_DIR}/AndroidManifest.xml")

    MESSAGE(STATUS "Android build: use <PATH_TO_NDK>/ndk-build NDK_APPLICATION_MK=Application.mk")
ENDIF(ANDROID)
